package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.jms.JMSException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.managerplat.model.OrderInfo;
import com.managerplat.model.Orders;
import com.managerplat.model.UserInfo;
import com.managerplat.service.UserManager;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

@Controller
public class UserController {
	@Autowired(required = true)
	private UserManager um;
	public static List<OrderInfo> orderList = null;
	@RequestMapping(value = "/userRegist", method = RequestMethod.POST) 
	@ResponseBody
	public int userRegist(@RequestBody String user) throws JsonParseException, JsonMappingException, IOException {

		JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(user);
		UserInfo u = (UserInfo) JSONObject.toBean(jsonObject, UserInfo.class);
		int id = -1;
		System.out.println(u.getUserName()+" "+u.getUserPassword()+" "+u.getUserPhone());
		id = um.userRegister(u);
		return id;
	}

	@RequestMapping(value = "/sendOrder", method = RequestMethod.POST)
	@ResponseBody
	public String getOrders(@RequestBody String jsonArrStr) {
		
		 JSONArray jsonArr = JSONArray.fromObject(jsonArrStr);
		 List<OrderInfo> list = new ArrayList();  
		 System.out.println(jsonArr.size());
	     	for (int i = 0; i < jsonArr.size(); i++) 
	     	{  
	            list.add((OrderInfo) JSONObject.toBean(jsonArr.getJSONObject(i), OrderInfo.class));  
	        
	     	}  
		um.tranlateOrders(list);
		Orders order = new Orders();
		order.setList(list);
		try {
			um.sendOrdersToJMS(order);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "success";
	}
	@RequestMapping("/getOrders")
	@ResponseBody
	public JSONArray getOrders(int sellerId,int isSuc,@ModelAttribute("orderInfo") JSONArray orderArray)
	{
		if(orderList == null || orderList.isEmpty())
		{
			orderList = um.sendOrderInfoToSellers(sellerId, isSuc);
		}
		orderArray = (JSONArray) JSONSerializer.toJSON(orderList);
		return orderArray;
	}
	@RequestMapping(value = "/userLogin")
	@ResponseBody
	public int userLogin(@RequestBody String user) {
		
		JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(user);
		UserInfo u = (UserInfo) JSONObject.toBean(jsonObject, UserInfo.class);
		return um.userLogin(u.getUserName(),u.getUserPassword());
	}

}
